package com.yl.chainStore.entity.warehouse

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.Constants
import com.yl.users.common.NeedGerId
import com.yl.chainStore.common.enums.Status
import com.yl.common.annotation.NoArg
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.*

/**
 * <p>
 * 入库明细
 * </p>
 *
 * @author cxw
 * @since 2021-06-10
 */
@NoArg
@TableName("warehouse_in_order_detail")
class WarehouseInOrderDetail (

    /**
     * 入库明细
     */
    @TableId(value = "id", type = IdType.AUTO)
    var id: Long,

    /**
     * 入库订单id
     */
    @TableField("in_order_id")
    var inOrderId: Long  = -1,

    /**
     * 物品id
     */
    @TableField("item_id")
    var itemId: Long,

    @TableField(exist = false)
    var itemName: String? = null,

    @TableField(exist = false)
    var nickname: String? = null,

    @TableField(exist = false)
    var unit: String? = null,

    @TableField(exist = false)
    var specification: String? = null,

    /**
     * 单价
     */
    @TableField("good_price")
    var goodPrice: Long = 0,


    /**
     * 物品数量
     */
    @TableField("good_count")
    var goodCount: Int = 0,

    /**
     * 备注
     */
    @TableField("remark")
    var remark: String? = null,

    /**
     * 数据有效性
     */
    @TableField("enabled")
    var enabled: Int = 1,

    /**
     * 状态 1待定，2成功 3失败（备用）
     */
    @TableField("status")
    var status: Status = Status.UNKNOWN,

    /**
     * 品牌
     */
    @TableField(exist = false)
    var brand:String ?= null,

    /**
     * 生产日期
     */
    @TableField("production_date")
    var productionDate:Date ? = null,

    /**
     * 有效结束期
     */
    @TableField("period_of_validity")
    var periodOfValidity:Date ? = null,
){

    /**
     * 实际要显示的金额
     */
    @TableField(exist = false)
    var realGoodPrice: BigDecimal?= null
        get() {
            if(field == null){
                return BigDecimal(goodPrice.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
            }
            return field
        }
        set(value) {
            if(value != null){
                goodPrice = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
                field = value
            }
        }

    /**
     * 实际要显示的金额
     */
    @TableField(exist = false)
    var realGoodSumMoney: BigDecimal?= null
        get() {
            val sumMoney = goodPrice * goodCount
            if(field == null){
                return BigDecimal(sumMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
            }
            return field
        }
        set(value) {
            if(value != null){
               val  sumMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
                goodPrice = sumMoney/goodCount
                field = value
            }
        }

}
